package cn.hl.sfzy.controller;

import cn.hl.sfzy.entity.Systemuser;
import cn.hl.sfzy.service.SystemuserService;
import cn.hl.sfzy.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName SystemuserController
 * @Description TOOO
 * @Author Lilysi
 * @Date 2024//10//22//16:38
 * @Version 1.0
 */
@RestController
@RequestMapping("/Systemuser")
@CrossOrigin
public class SystemuserController {
    @Autowired
    private SystemuserService systemuserService;

    @PostMapping("/login")
    public Result login(@RequestBody Systemuser systemuser) {
        Systemuser user = systemuserService.getSystemuser(systemuser);
        if (user == null) {
                return Result.error("用户名或密码错误");
        }
        if (user.getPassword()==null) {
            return Result.error("用户名或密码错误");
        }
        return Result.success(user);
    }

    @GetMapping("/pageSelect")
    public Result pageSelect(@RequestParam(required = false,defaultValue = "1") int pageNum, @RequestParam(required = false,defaultValue = "5") int pageSize, Systemuser systemuser) {
        PageInfo<Systemuser> byPage = systemuserService.findByPage(pageNum, pageSize, systemuser);
        return Result.success(byPage);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        int delete = systemuserService.delete(id);
        return Result.success("删除成功");
    }
    @PostMapping("/insert")
    public Result insert(@RequestBody Systemuser systemuser) {
        int insert = systemuserService.insert(systemuser);
        return Result.success(insert);
    }


    @PutMapping
    public Result update(@RequestBody Systemuser systemuser) {
        int update = systemuserService.update(systemuser);
        return Result.success(update);
    }
}
